package com.easylive.web.controller;

import com.easylive.component.EsSearchComponent;
import com.easylive.component.RedisComponent;
import com.easylive.entity.constants.Constants;
import com.easylive.entity.dto.TokenUserInfoDto;
import com.easylive.entity.enums.*;
import com.easylive.entity.po.UserAction;
import com.easylive.entity.po.VideoInfo;
import com.easylive.entity.po.VideoInfoFile;
import com.easylive.entity.query.UserActionQuery;
import com.easylive.entity.query.VideoInfoFileQuery;
import com.easylive.entity.query.VideoInfoQuery;
import com.easylive.entity.vo.PaginationResultVO;
import com.easylive.entity.vo.ResponseVO;
import com.easylive.entity.vo.VideoInfoResultVO;
import com.easylive.exception.BusinessException;
import com.easylive.service.UserActionService;
import com.easylive.service.VideoInfoFileService;
import com.easylive.service.VideoInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/video")
@Validated
public class VideoController extends ABaseController{
    @Resource
    private VideoInfoService videoInfoService;
    @Resource
    private VideoInfoFileService videoInfoFileService;
    @Resource
    private UserActionService userActionService;
    @Autowired
    private RedisComponent redisComponent;
    @Autowired
    private EsSearchComponent esSearchComponent;

    @RequestMapping("/loadRecommendVideo")
    public ResponseVO loadRecommendVideo(){
        VideoInfoQuery videoInfoQuery = new VideoInfoQuery();
        videoInfoQuery.setOrderBy("create_time desc");
        videoInfoQuery.setQueryUserInfo(true);
        videoInfoQuery.setRecommendType(VideoRecommendTypeEnum.RECOMMED.getType());
        List<VideoInfo> recommendVideoList = videoInfoService.findListByParam(videoInfoQuery);

        return getSuccessResponseVO(recommendVideoList);

    }

    @RequestMapping("/loadVideo")
    public ResponseVO loadVideo(Integer pCategoryId,Integer categoryId,Integer pageNo){
        VideoInfoQuery videoInfoQuery = new VideoInfoQuery();
        videoInfoQuery.setpCategoryId(pCategoryId);
        videoInfoQuery.setCategoryId(categoryId);

        videoInfoQuery.setPageNo(pageNo);
        videoInfoQuery.setOrderBy("create_time desc");
        videoInfoQuery.setQueryUserInfo(true);
        videoInfoQuery.setRecommendType(VideoRecommendTypeEnum.NO_RECOMMEND.getType());
        PaginationResultVO<VideoInfo> recommendVideoList = videoInfoService.findListByPage(videoInfoQuery);

        return getSuccessResponseVO(recommendVideoList);

    }

    @RequestMapping("/getVideoInfo")
    public ResponseVO getVideoInfo(@NotEmpty String videoId){
        VideoInfo videoInfo = videoInfoService.getVideoInfoByVideoId(videoId);
        if(videoInfo==null){
            throw new BusinessException((ResponseCodeEnum.CODE_404));
        }
        VideoInfoResultVO videoInfoResultVO = new VideoInfoResultVO();
        videoInfoResultVO.setVideoInfo(videoInfo);
        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();
        if(tokenUserInfoDto!=null){
            UserActionQuery userActionQuery = new UserActionQuery();
            userActionQuery.setUserId(tokenUserInfoDto.getUserId());
            userActionQuery.setVideoId(videoId);

            userActionQuery.setActionTypeArray(new Integer[]{UserActionTypeEnum.VIDEO_LIKE.getType(),UserActionTypeEnum.VIDEO_COLLECT.getType(),UserActionTypeEnum.VIDEO_COIN.getType()});


            videoInfoResultVO.setUserActionList(userActionService.findListByParam(userActionQuery));
        }

        return getSuccessResponseVO(videoInfoResultVO);

    }

    @RequestMapping("/loadVideoPList")
    public ResponseVO loadVideoPList(@NotEmpty String videoId){
        VideoInfoFileQuery videoInfoFileQuery = new VideoInfoFileQuery();
        videoInfoFileQuery.setVideoId(videoId);
        videoInfoFileQuery.setOrderBy("file_index asc");
        List<VideoInfoFile> videoInfoFileList = videoInfoFileService.findListByParam(videoInfoFileQuery);
        return getSuccessResponseVO(videoInfoFileList);

    }

    @RequestMapping("/reportVideoPlayOnline")
    public ResponseVO reportVideoPlayOnline(@NotEmpty String fileId,@NotEmpty String deviceId){

        return getSuccessResponseVO(redisComponent.reportVideoPlayOnline(fileId,deviceId));

    }

    @RequestMapping("/search")
    public ResponseVO search(@NotEmpty String keyword,Integer orderType,Integer pageNo) throws IOException {
        //TODO记录搜索热词
        redisComponent.addHotKeyWordCount(keyword);
        PaginationResultVO<VideoInfo> search = esSearchComponent.search(true, keyword, pageNo, PageSize.SIZE20.getSize(), orderType);

        return  getSuccessResponseVO(search);
    }

    @RequestMapping("/getVideoRecommend")
    public ResponseVO getVideoRecommend(@NotEmpty String keyword,@NotEmpty String videoId) throws IOException {
        //TODO记录搜索热词
        List<VideoInfo> videoInfoList = esSearchComponent.search(false, keyword, 1, PageSize.SIZE10.getSize(), EsCountFieldEnum.VIDEO_PLAY.getType()).getList();
        videoInfoList = videoInfoList.stream().filter(item -> !item.getVideoId().equals(videoId)).collect(Collectors.toList());
        return  getSuccessResponseVO(videoInfoList);
    }

    @RequestMapping("/getSearchKeywordTop")
    public ResponseVO getSearchKeywordTop(){
       List<String> keywordList = redisComponent.getHotKeyWordList(Constants.Length_10);
       return getSuccessResponseVO(keywordList);
    }

    @RequestMapping("/loadHotVideoList")
    public ResponseVO loadHotVideoList(Integer pageNo){
        VideoInfoQuery videoInfoQuery = new VideoInfoQuery();
        videoInfoQuery.setPageNo(pageNo);
        videoInfoQuery.setQueryUserInfo(true);
        videoInfoQuery.setOrderBy("play_count desc");
        videoInfoQuery.setLastPlayHour(24);
        PaginationResultVO<VideoInfo> list = videoInfoService.findListByPage(videoInfoQuery);
        return getSuccessResponseVO(list);
    }


}
